{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-10-30T07:42:16.550905400Z",
     "start_time": "2023-10-30T07:42:16.519214500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "def add(a,b):\n",
    "    return a+b\n",
    "\n",
    "sum = add(1,5)\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "def greet():\n",
    "    print(\"Hello, World!\")\n",
    "\n",
    "greet()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T07:48:19.625098100Z",
     "start_time": "2023-10-30T07:48:19.618107300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，Alice!\n",
      "你好，Bob!\n"
     ]
    }
   ],
   "source": [
    "def greet(name):\n",
    "    \"\"\"\n",
    "    这个函数通过名字来向一个人打招呼。\n",
    "\n",
    "    参数:\n",
    "        name (str): 要打招呼的人的名字。\n",
    "    \"\"\"\n",
    "    print(f\"你好，{name}!\")\n",
    "\n",
    "# 调用函数，传递一个名字作为参数\n",
    "greet(\"Alice\")  # 输出 \"你好，Alice!\"\n",
    "greet(\"Bob\")    # 输出 \"你好，Bob!\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T13:52:58.633314800Z",
     "start_time": "2023-10-30T13:52:58.589948800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n"
     ]
    }
   ],
   "source": [
    "def area(length, width):\n",
    "    \"\"\"\n",
    "    这个函数用于计算矩形的面积。\n",
    "    参数:\n",
    "        length (float or int): 矩形的长度。\n",
    "        width (float or int): 矩形的宽度。\n",
    "    \"\"\"\n",
    "    number = length * width\n",
    "    print(number)\n",
    "\n",
    "area(10, 5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T14:02:26.719948900Z",
     "start_time": "2023-10-30T14:02:26.703005500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "长度:10\n",
      "宽度:5\n",
      "长度:10\n",
      "宽度:5\n"
     ]
    }
   ],
   "source": [
    "def area(length, width):\n",
    "    print(f\"长度:{length}\")\n",
    "    print(f\"宽度:{width}\")\n",
    "\n",
    "area(length=10, width=5)\n",
    "area(width=5,length=10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-30T14:12:01.749120Z",
     "start_time": "2023-10-30T14:12:01.739019200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "def add(a,b):\n",
    "    return a+b\n",
    "\n",
    "Sum = add(1,6)\n",
    "print(Sum)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T10:19:03.257776300Z",
     "start_time": "2023-11-03T10:19:03.241707200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 8, 10)\n",
      "6 8 10\n"
     ]
    }
   ],
   "source": [
    "def Sum(a,b,c):\n",
    "    return a+5,b+6,c+7\n",
    "\n",
    "a1 = Sum(1,2,3)\n",
    "print(a1)\n",
    "a2,b2,c2 = Sum(1,2,3)\n",
    "print(a2,b2,c2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-03T10:27:21.073626900Z",
     "start_time": "2023-11-03T10:27:21.052999300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索隆的年龄是22\n",
      "索隆的年龄是21\n"
     ]
    }
   ],
   "source": [
    "def Greed(name,age=22):\n",
    "    print(f\"{name}的年龄是{age}\")\n",
    "\n",
    "# 如果函数调用时Age没有传入值，则22为默认值。\n",
    "Greed(\"索隆\")\n",
    "\n",
    "# 如果传入参数时\n",
    "Greed(\"索隆\",21)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-04T05:34:29.610822100Z",
     "start_time": "2023-11-04T05:34:29.599827200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('hello', 'Java', 'Python', 'World')\n"
     ]
    }
   ],
   "source": [
    "def scol(*temp):\n",
    "\n",
    "    print(temp)\n",
    "\n",
    "scol(\"hello\",\"Java\",\"Python\",\"World\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T04:27:38.149608Z",
     "start_time": "2023-11-05T04:27:38.140515100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'hello', 'age': 22}\n"
     ]
    }
   ],
   "source": [
    "def World(**Hello):\n",
    "    print(Hello)\n",
    "\n",
    "World(name=\"hello\",age=22)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T04:35:47.609225Z",
     "start_time": "2023-11-05T04:35:47.601021700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "位置参数 arg1: Hello\n",
      "位置参数 arg2: World\n",
      "默认值参数 default_arg: Custom\n",
      "任意参数 args:\n",
      "Extra1\n",
      "Extra2\n",
      "任意关键字参数 kwargs:\n",
      "kwarg1: Value1\n",
      "kwarg2: Value2\n"
     ]
    }
   ],
   "source": [
    "def example_function(arg1, arg2, default_arg=\"default_value\", *args, **kwargs):\n",
    "    print(f\"位置参数 arg1: {arg1}\")\n",
    "    print(f\"位置参数 arg2: {arg2}\")\n",
    "    print(f\"默认值参数 default_arg: {default_arg}\")\n",
    "    print(\"任意参数 args:\")\n",
    "    for arg in args:\n",
    "        print(arg)\n",
    "    print(\"任意关键字参数 kwargs:\")\n",
    "    for key, value in kwargs.items():\n",
    "        print(f\"{key}: {value}\")\n",
    "\n",
    "example_function(\"Hello\", \"World\", \"Custom\", \"Extra1\", \"Extra2\", kwarg1=\"Value1\", kwarg2=\"Value2\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T04:42:02.566658900Z",
     "start_time": "2023-11-05T04:42:02.563383900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "global_variable = 10  # 这是一个全局变量\n",
    "\n",
    "def function1():\n",
    "    print(global_variable)  # 可以在函数内访问全局变量\n",
    "\n",
    "def function2():\n",
    "    global global_variable\n",
    "    global_variable = 20  # 修改全局变量的值\n",
    "\n",
    "function1()  # 输出：10\n",
    "function2()\n",
    "function1()  # 输出：20\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T04:48:04.348860400Z",
     "start_time": "2023-11-05T04:48:04.333739700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "def function_with_local_variable():\n",
    "    local_variable = 5  # 这是一个局部变量\n",
    "    print(local_variable)\n",
    "\n",
    "function_with_local_variable()  # 输出：5\n",
    "# 这里尝试访问局部变量会导致NameError\n",
    "# print(local_variable)  # 报错：NameError: name 'local_variable' is not defined"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "# 匿名函数计算两个数的和\n",
    "add = lambda x, y: x + y\n",
    "result = add(3, 5)  # 结果为8\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T05:07:29.246672200Z",
     "start_time": "2023-11-05T05:07:29.240268500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "# 匿名函数用于过滤\n",
    "numbers = [1, 2, 3, 4, 5, 6]\n",
    "even_numbers = list(filter(lambda x: x % 2 == 0, numbers))\n",
    "# 过滤出偶数，结果为[2, 4, 6]\n",
    "print(even_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T05:12:08.468977400Z",
     "start_time": "2023-11-05T05:12:08.442615700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# 使用map()函数应用匿名函数，将列表中的每个元素加倍\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "doubled_numbers = map(lambda x: x * 2, numbers)\n",
    "# 转换迭代器为列表\n",
    "doubled_numbers_list = list(doubled_numbers)\n",
    "print(doubled_numbers_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T05:16:18.533828200Z",
     "start_time": "2023-11-05T05:16:18.523620600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['fig', 'date', 'apple', 'cherry', 'banana']\n",
      "[5, 4, 3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "# 使用sorted()函数按元素的长度对字符串列表进行排序\n",
    "words = [\"apple\", \"cherry\", \"banana\", \"date\", \"fig\"]\n",
    "sorted_words = sorted(words, key=lambda word: len(word))\n",
    "# sorted_words 是一个新的列表，按单词长度排序，结果为[\"fig\", \"date\", \"apple\", \"cherry\", \"banana\"]\n",
    "print(sorted_words)\n",
    "\n",
    "# 使用sorted()函数按元素的倒序对数字列表进行排序\n",
    "numbers = [5, 3, 1, 4, 2]\n",
    "sorted_numbers = sorted(numbers, key=lambda x: -x)\n",
    "# sorted_numbers 是一个新的列表，按倒序排序，结果为[5, 4, 3, 2, 1]\n",
    "print(sorted_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-05T05:29:35.726370900Z",
     "start_time": "2023-11-05T05:29:35.708459600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
